class Fragment < ActiveRecord::Base
  belongs_to :novel
  belongs_to :user
  belongs_to :chapter
  has_many :vote
  has_many :comment
  has_one :merge

  validates_presence_of  :content,:message=>"我们不接受无字的续篇，天书不应该出现在人间！"
#  validates_length_of :content, :minimum=>1,:message=>"我们不接受无字的续篇，天书不应该出现在人间！"
  
  FRAGMENT_MERGE = 1 # Means this fragment is merged to novel.

  def can_be_merged
    #TODO What's the merge condition?'
    stas = vote_statistical
    return stas[0]>10
  end

  def vote_statistical
    res = [0, 0, 0]
    if self.vote
      self.vote.each do |vote|
        case vote.level
        when Vote::FRAGMENT_VOTE_GOOD
          res[0]+=1
        when Vote::FRAGMENT_VOTE_BAD
          res[1]+=1
        when Vote::FRAGMENT_VOTE_MORE_BAD
          res[2]+=1
        else
        end
      end
    end
    res
  end
end
